
package com.pp.admin.common.server.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.pp.admin.common.client.beans.BaseDomain;
import com.pp.admin.common.server.PersistenceManagerFactory;

/**
 * The persistent class for the PP_DOMAINS database table.
 */
@PersistenceCapable( identityType = IdentityType.APPLICATION )
public class Domain extends Base implements Serializable {
	
	private static final long	serialVersionUID	= 1L;
	
	public static final byte	STATUS_INCATIVE		= 1;
	
	public static final byte	STATUS_ACTIVE		= 2;
	
	@PrimaryKey
	@Persistent
	private Key					id;
	
	@Persistent
	private String				domainID;
	
	@Persistent
	private String				uesrFriendlyName;
	
	@Persistent
	private User				owner;
	
	@Persistent
	private String				homeURL;
	
	@Persistent
	private byte				status;
	
	@Persistent
	private String				notes;
	
	@Persistent
	private Key					createdBy;
	
	@Persistent
	private Date				createdByTime;
	
	@Persistent
	private Key					lastModefiedBy;
	
	@Persistent
	private Date				lastModefiedByTime;
	
	// bi-directional many-to-one association to UsersDomain
	@Persistent( mappedBy = "domain" )
	@Element( dependent = "true" )
	private List<UsersDomain>	usersDomains		= new ArrayList<UsersDomain>();
	
	// bi-directional many-to-one association to Permission
	@Persistent( mappedBy = "domain" )
	@Element( dependent = "true" )
	private List<Permission>	permissions			= new ArrayList<Permission>();
	
	// bi-directional many-to-one association to Role
	@Persistent( mappedBy = "domain" )
	@Element( dependent = "true" )
	private List<Role>			roles				= new ArrayList<Role>();
	
	public Domain() {

	}
	
	/**
	 * @return the id
	 */
	public Key getId() {

		return id;
	}
	
	/**
	 * @param id
	 *            the id to set
	 */
	public void setId( Key id ) {

		this.id = id;
	}
	
	/**
	 * @return the domainID
	 */
	public String getDomainID() {

		return domainID;
	}
	
	/**
	 * @param domainID
	 *            the domainID to set
	 */
	public void setDomainID( String domainID ) {

		this.domainID = domainID;
	}
	
	/**
	 * @return the owner
	 */
	public User getOwner() {

		return owner;
	}
	
	/**
	 * @param owner
	 *            the owner to set
	 */
	public void setOwner( User owner ) {

		this.owner = owner;
	}
	
	/**
	 * @return the homeURL
	 */
	public String getHomeURL() {

		return homeURL;
	}
	
	/**
	 * @param homeURL
	 *            the homeURL to set
	 */
	public void setHomeURL( String homeURL ) {

		this.homeURL = homeURL;
	}
	
	/**
	 * @return the status
	 */
	public byte getStatus() {

		return status;
	}
	
	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus( byte status ) {

		this.status = status;
	}
	
	/**
	 * @return the notes
	 */
	public String getNotes() {

		return notes;
	}
	
	/**
	 * @param notes
	 *            the notes to set
	 */
	public void setNotes( String notes ) {

		this.notes = notes;
	}
	
	/**
	 * @return the usersDomains
	 */
	public List<UsersDomain> getUsersDomains() {

		return usersDomains;
	}
	
	/**
	 * @param usersDomains
	 *            the usersDomains to set
	 */
	public void setUsersDomains( List<UsersDomain> usersDomains ) {

		this.usersDomains = usersDomains;
	}
	
	/**
	 * @return the permissions
	 */
	public List<Permission> getPermissions() {

		return permissions;
	}
	
	/**
	 * @param permissions
	 *            the permissions to set
	 */
	public void setPermissions( List<Permission> permissions ) {

		this.permissions = permissions;
	}
	
	/**
	 * @return the roles
	 */
	public List<Role> getRoles() {

		return roles;
	}
	
	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles( List<Role> roles ) {

		this.roles = roles;
	}
	
	/**
	 * @return the uesrFriendlyName
	 */
	public String getUesrFriendlyName() {

		return uesrFriendlyName;
	}
	
	/**
	 * @param uesrFriendlyName
	 *            the uesrFriendlyName to set
	 */
	public void setUesrFriendlyName( String uesrFriendlyName ) {

		this.uesrFriendlyName = uesrFriendlyName;
	}
	
	/**
	 * @return the createdBy
	 */
	@Override
	public User getCreatedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.createdBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	@Override
	public void setCreatedBy( Key createdBy ) {

		this.createdBy = createdBy;
	}
	
	/**
	 * @return the createdByTime
	 */
	@Override
	public Date getCreatedByTime() {

		return createdByTime;
	}
	
	/**
	 * @param createdByTime
	 *            the createdByTime to set
	 */
	@Override
	public void setCreatedByTime( Date createdByTime ) {

		this.createdByTime = createdByTime;
	}
	
	/**
	 * @return the lastModefiedBy
	 */
	@Override
	public User getLastModefiedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.lastModefiedBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param lastModefiedBy
	 *            the lastModefiedBy to set
	 */
	@Override
	public void setLastModefiedBy( Key lastModefiedBy ) {

		this.lastModefiedBy = lastModefiedBy;
	}
	
	/**
	 * @return the lastModefiedByTime
	 */
	@Override
	public Date getLastModefiedByTime() {

		return lastModefiedByTime;
	}
	
	/**
	 * @param lastModefiedByTime
	 *            the lastModefiedByTime to set
	 */
	@Override
	public void setLastModefiedByTime( Date lastModefiedByTime ) {

		this.lastModefiedByTime = lastModefiedByTime;
	}
	
	public com.pp.admin.common.client.beans.Domain export() {

		com.pp.admin.common.client.beans.Domain dom = new com.pp.admin.common.client.beans.Domain();
		exportBaseDomain( dom );
		dom.setOwner( this.owner.exportBaseUser() );
		dom.setStatus( this.status );
		return dom;
	}
	
	public BaseDomain exportBaseDomain() {

		BaseDomain bsDomain = new BaseDomain();
		return exportBaseDomain( bsDomain );
	}
	
	public BaseDomain exportBaseDomain( BaseDomain bsDomain ) {

		bsDomain.setDomainID( this.domainID );
		bsDomain.setHomeURL( this.homeURL );
		bsDomain.setNotes( this.notes );
		bsDomain.setUesrFriendlyName( this.uesrFriendlyName );
		return bsDomain;
	}
	
	public Domain getDomainByDomainID( String userID, String domainID ) {

		Domain domain = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			Key domainkey = generateKey( userID, domainID );
			domain = pm.getObjectById( Domain.class, domainkey );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return domain;
	}
	
	/**
	 * @param userID
	 * @param id
	 * @return
	 */
	public static Key generateKey( String userID, String id ) {

		Key userKey = User.generateKey( userID );
		Key domainKey = new KeyFactory.Builder( userKey ).addChild( Domain.class.getSimpleName(), id ).getKey();
		return domainKey;
	}
	
}
